﻿Imports IICModel
Namespace PeriodProcessing

    ''' <summary>
    ''' Class that saves results of processor to the database
    ''' </summary>
    ''' <remarks></remarks>
    Public Class DBProcessorPersistor

        Public Sub persistToDatabase(ByVal pproc As PeriodProcessor)

            'flag to indicate whether we started a transaction
            Dim transStarted As Boolean = False

            Try
                'check if we are already in transaction, if not then start one
                If ModelContext.CurrentDBUtils().inTrans = False Then
                    ModelContext.beginTrans()
                    transStarted = True
                End If

                Dim cmdb As New ClassMemberDBMapper
                Dim csrdb As New CertificationSatisfiedRequirementDBMapper

                Dim dsrdb As New DiplomaSatisfiedRequirementDBMapper
                Dim cdv As New CertificationEarnedDBMapper()
                Dim cdd As New DiplomaEarnedDBMapper()
                Dim ppdb As New PeriodProcessDBMapper()

                'Debug.WriteLine("-----------------------------------")
                Dim pp As PeriodProcess = pproc.PeriodProcess
                pp.PeriodProcessUser = ModelContext.Current.CurrentUserName
                ppdb.save(pp)

                'update each record with the new PeriodProcessId created
                pproc.ExamsStudentProcessor.classMembersProcessed.ForEach(Sub(x)
                                                                              'we check and update only of PeriodProcessId is null
                                                                              'because we may have loaded processed items from 
                                                                              'previous periods and we do not want to change the 
                                                                              'period process id
                                                                              If x.PeriodProcessId Is Nothing Then
                                                                                  x.PeriodProcessId = pp.PeriodProcessId
                                                                              End If

                                                                          End Sub)

                cmdb.saveList(pproc.ExamsStudentProcessor.classMembersProcessed)

                pproc.diplomasEarned.ForEach( _
                        Sub(des)
                            If des.EarnedPeriodProcessId Is Nothing Then
                                des.EarnedPeriodProcessId = pp.PeriodProcessId
                            End If

                            For Each cesr As DiplomaSatisfiedRequirement In des.DiplomaSatisfiedRequirements
                                If cesr.SatisfiedPeriodProcessId Is Nothing Then
                                    cesr.SatisfiedPeriodProcessId = pp.PeriodProcessId
                                End If

                                If cesr.certificationEarned.EarnedPeriodProcessId Is Nothing Then
                                    cesr.certificationEarned.EarnedPeriodProcessId = pp.PeriodProcessId
                                End If

                                For Each csrqs As CertificationSatisfiedRequirement In cesr.certificationEarned.CertificationSatisfiedRequirements
                                    If csrqs.SatisfiedPeriodProcessId Is Nothing Then
                                        csrqs.SatisfiedPeriodProcessId = pp.PeriodProcessId
                                    End If
                                Next

                            Next

                        End Sub)

                cdd.saveList(pproc.diplomasEarned)

                pproc.certificatesEarned.ForEach( _
                    Sub(ces)

                        If ces.EarnedPeriodProcessId Is Nothing Then
                            ces.EarnedPeriodProcessId = pp.PeriodProcessId
                        End If

                        For Each cesr As CertificationSatisfiedRequirement In ces.CertificationSatisfiedRequirements
                            If cesr.SatisfiedPeriodProcessId Is Nothing Then
                                cesr.SatisfiedPeriodProcessId = pp.PeriodProcessId
                            End If
                        Next

                    End Sub)

                cdv.saveList(pproc.certificatesEarned)

                pproc.unusedCertSatisfiedRequirements.ForEach(Sub(cesr)
                                                                  If cesr.SatisfiedPeriodProcessId Is Nothing Then
                                                                      cesr.SatisfiedPeriodProcessId = pp.PeriodProcessId
                                                                  End If
                                                              End Sub)
                csrdb.saveList(pproc.unusedCertSatisfiedRequirements)

                pproc.unusedDiplomaSatisfiedRequirements.ForEach(Sub(cesr)
                                                                     If cesr.SatisfiedPeriodProcessId Is Nothing Then
                                                                         cesr.SatisfiedPeriodProcessId = pp.PeriodProcessId
                                                                     End If
                                                                 End Sub)

                dsrdb.saveList(pproc.unusedDiplomaSatisfiedRequirements)


                If transStarted = True Then
                    'commit here if we started a txn
                    ModelContext.commitTrans()
                End If

            Finally
                If transStarted = True Then
                    'rollback here if we started a txn
                    ModelContext.rollbackTrans()
                End If

            End Try
        End Sub


    End Class
End Namespace
